package com.chenjian.codingstylespringsecurity.security.browser;

import com.chenjian.codingstylespringsecurity.security.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author chenjian
 * @Package
 * @Description:
 * @date 2018/12/3 15:45
 */
@Configuration
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SecurityProperties securityProperties;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.formLogin()//表单登陆
                .loginPage("/authentication/require")
                .and()
                .authorizeRequests()//身份认证
                .mvcMatchers("/authentication/require", securityProperties.getBrowserSecurityProperties().getLoginpage()).permitAll()
                .anyRequest()//任何请求
                .authenticated();//都需要身份认证
    }

    @Bean
    public PasswordEncoder passwordEncoder() {

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

        return passwordEncoder;
    }
}
